package edu.syr.iis.web.controller;

import java.beans.PropertyEditorSupport;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import edu.syr.iis.web.model.DialogueActLabel;
import edu.syr.iis.web.model.DialogueActLabelManager;

public class DialogueActLabelPropertyEditor  extends PropertyEditorSupport {
	
	DialogueActLabelManager manager;
	private static Log log = LogFactory.getLog(DialogueActLabelPropertyEditor.class);
		
	
	public DialogueActLabelPropertyEditor() {		
		//super();
		// this.manager = new DialogueActLabelManager();
		this(new DialogueActLabelManager());
	}

	public DialogueActLabelPropertyEditor(DialogueActLabelManager manager) {
		super();
		this.manager = manager;
	}
	
	@Override
	public void setAsText(String text)  throws IllegalArgumentException 
	{
		log.error("Entering: setAsText() with: " + text);
		
		if (text.equals("null"))
		{
			setValue(null);
		} 
		else
		{
			int id = Integer.parseInt(text);
			DialogueActLabel value = manager.getDialogueActLabel(id);

			if (value == null) {
				throw new IllegalArgumentException("Nooo object found with id: " + id);
			}
			setValue(value);
		}
	}
	
	@Override
	public String getAsText() {			
		 
		DialogueActLabel value = (DialogueActLabel) getValue();
		String text = "null";
		
		System.out.println("Entering getAsText() with " + value);
		
		if (value != null)
			text = Integer.toString(value.getDialogueActLabelId());
		
		System.out.println("Returning: " + text);
		return text;
	}

	public DialogueActLabelManager getManager() {
		return manager;
	}

	public void setManager(DialogueActLabelManager manager) {
		this.manager = manager;
	}
	
	

}
